package com.zhengxing.jienci.web.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import com.zhengxing.jienci.common.util.FastJsonUtil;
import com.zhengxing.jienci.web.context.SecurityContext;

public class CurrentUserProcessorFilter extends OncePerRequestFilter{
	
	private static final Logger log = LoggerFactory.getLogger(CurrentUserProcessorFilter.class);
	
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String tokenInfo = request.getHeader("tokenInfo");
		if(StringUtils.isNotEmpty(tokenInfo)) {
			Map token = FastJsonUtil.toBean(tokenInfo, Map.class);
			SecurityContext.getUserId(token.get("user_name"));
		}
		log.info("当前登录用户为：{}", tokenInfo);
		filterChain.doFilter(request, response);
	}
	
}
